/**
 * AbstractImageFilterControl.java
 * 
 * @date: 2012-02-08
 * @author: Xiaoyu Guo (MUN# 200982155)
 * This file is part of the course project for COMP 6752, 
 * Application of Computer Graphics. All the file is written
 * by myself.
 */
package mun.cs6752.project.filter.abstractClasses;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.JButton;
import javax.swing.JPanel;

import mun.cs6752.project.filter.interfaces.IImageFilter;
import mun.cs6752.project.filter.interfaces.IImageFilterControl;
import mun.cs6752.project.filter.interfaces.IImageFilterParameter;
import mun.cs6752.project.filter.interfaces.IImageProvider;

/**
 * @author Xiaoyu Guo
 *
 */
public abstract class AbstractImageFilterControl
    extends JPanel 
    implements IImageFilterControl {
    private static final long serialVersionUID = -7048210293309712079L;

    protected IImageFilter filter;
    protected IImageProvider provider;
    
    public AbstractImageFilterControl(IImageProvider provider, IImageFilter filter){
        this.provider = provider;
        this.filter = filter;
        
        initUI();
        initValue();
    }
    
    protected void initValue() {
        // TODO Auto-generated method stub
        
    }

    protected void initUI() {
        // TODO Auto-generated method stub
        this.setLayout(new BorderLayout());
        
        JPanel pnlControls = new JPanel();
        this.add(pnlControls, BorderLayout.SOUTH);
        
        JButton btnPreview = new JButton("Preview");
        btnPreview.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                preview();
            }
        });
        pnlControls.add(btnPreview);
        
        JButton btnDone = new JButton("Done");
        btnDone.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                done();
            }
        });
        pnlControls.add(btnDone);
        
        JButton btnCancel = new JButton("Cancel");
        pnlControls.add(btnCancel);
    }

    /* (non-Javadoc)
     * @see mun.cs6752.project.filter.interfaces.IImageFilterControl#preview()
     */
    @Override
    public void preview() {
        provider.setTargetImage(filter.execute(provider.getPreviewImage(), getParameter()));
    }

    /* (non-Javadoc)
     * @see mun.cs6752.project.filter.interfaces.IImageFilterControl#done()
     */
    @Override
    public void done() {
        IImageFilterParameter p = this.getParameter();
        BufferedImage destImage = filter.execute(provider.getSourceImage(), p);
        provider.setTargetImage(destImage);
    }

    /* (non-Javadoc)
     * @see mun.cs6752.project.filter.interfaces.IImageFilterControl#cancel()
     */
    @Override
    public void cancel() {
        // do nothing
        provider.setTargetImage(provider.getSourceImage());
    }

}
